/*
 * @Author: 闫聪敏
 * @Email: 
 * @Date: 2020-02-24 08:33:59
 * @Descripttion: 
 */
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators } from "@angular/forms";
import { WorkManageService } from './work-manage.service';
import { IfStmt } from '@angular/compiler';
import { NzModalRef, NzModalService } from 'ng-zorro-antd/modal';
import { HCommonService } from 'src/app/h-common/h-common.service';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
    selector: 'app-work-manage',
    templateUrl: './work-manage.component.html',
    styleUrls: ['./work-manage.component.scss']
})
export class WorkManageComponent implements OnInit {
    private erpConfirmModal: NzModalRef;
    searchKey: string = '';
    pageIndex: any = 1;//提交的页码
    pagesize: any = 10;//table每页条数
    totalSize: any = '';//总共条数
    code: string = "";
    isIndeterminate = false;
    listData = [];
    isAllChecked = false;
    bornRoles = {};
    constructor(
        private workManageService: WorkManageService,
        private modal: NzModalService,
        private commonService: HCommonService,
        private message: NzMessageService
    ) { }

    ngOnInit() {
        /* 获取权限 */
        this.bornRoles = this.commonService.getRoles("command");
        this.getSearchList()
        this.getUserTree()
    }
    /* 分页切换 */
    clickPage(pageIndex) {
        this.pageIndex = pageIndex
        this.submitSearch()
    }
    // 提交搜索内容
    submitSearch() {
        this.getSearchList()
    }
    // 获取搜索后的列表
    getSearchList() {
        let data = {
            code: "",
            content: this.searchKey,
            page: this.pageIndex,
            size: this.pagesize,
        }
        this.workManageService.apiList(data).subscribe(res => {
            res.content.forEach(item => {
                item.checked = false;
                item.manager = Number(item.manager)
            })
            this.listData = res.content;
            this.totalSize = res.totalElements
        });
    }

    checkAll(value: boolean): void {
        this.listData.forEach(item => {
            item.checked = value;
        })
    }
    checkSingle() {
        let _isAllCheck = true;
        this.listData.forEach(item => {
            if (!item.checked) {
                _isAllCheck = false;
            }
        })
        this.isAllChecked = _isAllCheck;
    }
    save() {
        let _dataJson = []
        this.listData.forEach(item => {
            if (item.checked) {
                _dataJson.push({
                    code: "",
                    content: item.content,
                    id: item.id,
                    manager: item.manager,
                    remarks: item.remarks
                })
            }
        })
        if (_dataJson.length == 0) {
            this.modal.warning({
                nzTitle: "修改提示",
                nzContent: "请选择要修改的项"
            });
            return false;
        }
        this.workManageService.edit(_dataJson).subscribe(res => {
            this.isAllChecked = false;
            this.getSearchList();
            this.modal.success({
                nzTitle: '保存提示',
                nzContent: '保存成功'
            });
        })
    }
    private isLoading=false;
    handleShowErpModal(): void {
        this.erpConfirmModal = this.modal.confirm({
            nzTitle: '获取ERP数据提示',
            nzContent: '确定要获取数据吗？',
            nzOnOk: () =>
            {
                this.isLoading=true;
                this.workManageService.erpData().subscribe(res => {
                    this.isLoading=false;
                    this.message.info("获取成功");
                    this.getSearchList();
                },err=>{
                    // let errorMessage = err.error.message
                    // this.message.create("error", errorMessage)
                    this.isLoading=false;
                });
            }
        });
    }

    // zxh 项目负责人改成下拉树形菜单
    nodes:any = []
    onChange(e){
        console.log(e)
    }
    getUserTree(){
        this.workManageService.getUserTree().subscribe(res=>{
            let _data = this.userTreeData(res)
            this.nodes = _data
        },err=>{

        })
    }
    userTreeData(data){
        data.forEach(item => {
            item.title = item.name
            item.key = item.id
            if(item.type=='user'){
                item.disabled = false
            }else{
                item.disabled = true
            }
            if(item.children){
                if(item.children==''){
                    item.isLeaf = true
                }
                this.userTreeData(item.children)
            }else{
                item.isLeaf = true
            }
        })
        return data
    }

}
